﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;

namespace Wpf.CommonTools.SystemExtension
{
    public static partial class WatermarkService
    {
        /// <summary>
        /// Watermark Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty TitlemarkProperty = DependencyProperty.RegisterAttached(
           "Titlemark",
           typeof(object),
           typeof(WatermarkService),
           new FrameworkPropertyMetadata((object)null, new PropertyChangedCallback(OnTitlemarkChanged)));

        /// <summary>
        /// Gets the Watermark property.  This dependency property indicates the watermark for the control.
        /// </summary>
        /// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
        /// <returns>The value of the Watermark property</returns>
        public static object GetTitlemark(DependencyObject d)
        {
            return (object)d.GetValue(TitlemarkProperty);
        }

        /// <summary>
        /// Sets the Watermark property.  This dependency property indicates the watermark for the control.
        /// </summary>
        /// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
        /// <param name="value">value of the property</param>
        public static void SetTitlemark(DependencyObject d, object value)
        {
            d.SetValue(TitlemarkProperty, value);
        }

        /// <summary>
        /// Handles changes to the Watermark property.
        /// </summary>
        /// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
        /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
        private static void OnTitlemarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Control control = (Control)d;
            control.Loaded += Title_Loaded;
            //if (control is TextBox)
            //{
            //    TextBox tb = control as TextBox;
            //    int addV = 0, addH = 0;
            //    if (tb.VerticalScrollBarVisibility == ScrollBarVisibility.Visible)
            //    {
            //        addH = 20;
            //    }
            //    if (tb.HorizontalScrollBarVisibility == ScrollBarVisibility.Visible)
            //    {
            //        addV = 20;
            //    }
            //    control.Opacity = 0.3;
            //    control.Margin = new Thickness(
            //        control.Margin.Left,
            //        control.Margin.Top,
            //        control.Margin.Right + addH,
            //        control.Margin.Bottom + addV);
            //}
        }

        #region Event Handlers

        /// <summary>
        /// Handle the Loaded and LostFocus event on the control
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
        private static void Title_Loaded(object sender, RoutedEventArgs e)
        {
            Control control = (Control)sender;
            ShowTitlemark(control);
        }


        #endregion

        #region Helper Methods

        /// <summary>
        /// Show the watermark on the specified control
        /// </summary>
        /// <param name="control">Control to show the watermark on</param>
        private static void ShowTitlemark(Control control)
        {
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(control);

            // layer could be null if control is no longer in the visual tree
            if (layer != null)
            {
                layer.Add(new WatermarkAdorner(control, GetTitlemark(control)));
            }
        }



        #endregion
    }
}
